package com.wind.taobao.api.trade.trade.convert
{
	import com.wind.taobao.api.trade.trade.model.Trade;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Trade转换器
	 * @author fxy
	 *
	 */
	public class TradeConvert
	{
		/**
		 * 转换Trade集合
		 */
		public static function convertTrades(value:Object, type:String="json"):ArrayCollection
		{
			var trades:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							trades.addItem(convertTrade((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return trades;
		}

		/**
		 * 转换单个Trade对象
		 * value:XML 或 JSON
		 */
		public static function convertTrade(value:Object, type:String="json", trade:Trade=null):Trade
		{
			if (trade == null)
				trade=new Trade();
			if (value == null)
				return trade;
			if (type == TaobaoConstants.XML)
			{
				trade=convertXMLToTrade(XML(value), trade);
			}
			else
			{
				trade=convertJSONToTrade(value, trade);
			}
			return trade;
		}

		/**
		 * JSON转换为Trade
		 */
		private static function convertJSONToTrade(json:Object, trade:Trade):Trade
		{
			if (json.hasOwnProperty(TaobaoConstants.SELLER_NICK))
			{
				trade.seller_nick=json[TaobaoConstants.SELLER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_NICK))
			{
				trade.buyer_nick=json[TaobaoConstants.BUYER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				trade.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.TYPE))
			{
				trade.type=json[TaobaoConstants.TYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				trade.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.IID))
			{
				trade.iid=json[TaobaoConstants.IID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PRICE))
			{
				trade.price=json[TaobaoConstants.PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PIC_PATH))
			{
				trade.pic_path=json[TaobaoConstants.PIC_PATH];
			}
			if (json.hasOwnProperty(TaobaoConstants.NUM))
			{
				trade.num=json[TaobaoConstants.NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.TID))
			{
				trade.tid=json[TaobaoConstants.TID];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_MESSAGE))
			{
				trade.buyer_message=json[TaobaoConstants.BUYER_MESSAGE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SID))
			{
				trade.sid=json[TaobaoConstants.SID];
			}
			if (json.hasOwnProperty(TaobaoConstants.SHIPPING_TYPE))
			{
				trade.shipping_type=json[TaobaoConstants.SHIPPING_TYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.ALIPAYNO))
			{
				trade.alipay_no=json[TaobaoConstants.ALIPAYNO];
			}
			if (json.hasOwnProperty(TaobaoConstants.PAYMENT))
			{
				trade.payment=json[TaobaoConstants.PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.DISCOUNT_FEE))
			{
				trade.discount_fee=json[TaobaoConstants.PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.ADJUST_FEE))
			{
				trade.adjust_fee=json[TaobaoConstants.ADJUST_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SNAPSHOT_URL))
			{
				trade.snapshot_url=json[TaobaoConstants.SNAPSHOT_URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				trade.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_RATE))
			{
				trade.seller_rate=json[TaobaoConstants.SELLER_RATE];//已转换Boolean
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_RATE))
			{
				trade.buyer_rate=json[TaobaoConstants.BUYER_RATE];//已转换Boolean
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_MEMO))
			{
				trade.buyer_memo=json[TaobaoConstants.BUYER_MEMO];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_MEMO))
			{
				trade.seller_memo=json[TaobaoConstants.SELLER_MEMO];
			}
			if (json.hasOwnProperty(TaobaoConstants.PAY_TIME))
			{
				trade.pay_time=json[TaobaoConstants.PAY_TIME];
			}
			if (json.hasOwnProperty(TaobaoConstants.END_TIME))
			{
				trade.end_time=json[TaobaoConstants.END_TIME];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				trade.modified=json[TaobaoConstants.MODIFIED];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_OBTAIN_POINT_FEE))
			{
				trade.buyer_obtain_point_fee=json[TaobaoConstants.BUYER_OBTAIN_POINT_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.POINT_FEE))
			{
				trade.point_fee=json[TaobaoConstants.POINT_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.REAL_POINT_FEE))
			{
				trade.real_point_fee=json[TaobaoConstants.REAL_POINT_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.TOTAL_FEE))
			{
				trade.total_fee=json[TaobaoConstants.TOTAL_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.POST_FEE))
			{
				trade.post_fee=json[TaobaoConstants.POST_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_ALIPAY_NO))
			{
				trade.buyer_alipay_no=json[TaobaoConstants.BUYER_ALIPAY_NO];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_NAME))
			{
				trade.receiver_name=json[TaobaoConstants.RECEIVER_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_STATE))
			{
				trade.receiver_state=json[TaobaoConstants.RECEIVER_STATE];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_CITY))
			{
				trade.receiver_city=json[TaobaoConstants.RECEIVER_CITY];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_DISTRICT))
			{ //此属性需验证返回
				trade.receiver_district=json[TaobaoConstants.RECEIVER_DISTRICT];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_ADDRESS))
			{
				trade.receiver_address=json[TaobaoConstants.RECEIVER_ADDRESS];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_ZIP))
			{
				trade.receiver_zip=json[TaobaoConstants.RECEIVER_ZIP];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_MOBILE))
			{
				trade.receiver_mobile=json[TaobaoConstants.RECEIVER_MOBILE];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_PHONE))
			{
				trade.receiver_phone=json[TaobaoConstants.RECEIVER_PHONE];
			}
			if (json.hasOwnProperty(TaobaoConstants.CONSIGN_TIME))
			{
				trade.consign_time=json[TaobaoConstants.CONSIGN_TIME];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_EAMIL))
			{
				trade.buyer_email=json[TaobaoConstants.BUYER_EAMIL];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMMISSION_FEE))
			{
				trade.commission_fee=json[TaobaoConstants.COMMISSION_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_ALIPAY_NO))
			{
				trade.seller_alipay_no=json[TaobaoConstants.SELLER_ALIPAY_NO];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_MOBILE))
			{
				trade.seller_mobile=json[TaobaoConstants.SELLER_MOBILE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_PHONE))
			{
				trade.seller_phone=json[TaobaoConstants.SELLER_PHONE];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_NAME))
			{
				trade.seller_name=json[TaobaoConstants.SELLER_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_EAMIL))
			{
				trade.seller_email=json[TaobaoConstants.SELLER_EAMIL];
			}
			if (json.hasOwnProperty(TaobaoConstants.AVAILABLE_CONFIRM_FEE))
			{
				trade.available_confirm_fee=json[TaobaoConstants.AVAILABLE_CONFIRM_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.HAS_POST_FEE))
			{
				trade.has_postFee=json[TaobaoConstants.HAS_POST_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVED_PAYMENT))
			{
				trade.received_payment=json[TaobaoConstants.RECEIVED_PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.COD_FEE))
			{
				trade.cod_fee=json[TaobaoConstants.COD_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.TIMEOUT_ACTION_TIME))
			{
				trade.timeout_action_time=json[TaobaoConstants.TIMEOUT_ACTION_TIME];
			}
			if (json.hasOwnProperty(TaobaoConstants.ORDERS))
			{
				trade.orders=OrderConvert.convertOrders(json[TaobaoConstants.ORDERS]);
			}
			return trade;
		}



		/**
		 * XML转换为Trade
		 */
		private static function convertXMLToTrade(value:XML, trade:Trade):Trade
		{
			return trade;
		}


	}
}